package com.greenline.search.test.dao;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;

/**
 * Created with IntelliJ IDEA.
 * User: liulei
 * Date: 13-9-24
 * Time: 下午5:21
 * To change this template use File | Settings | File Templates.
 */
public class MybatisSession {
    private Logger logger = LoggerFactory.getLogger(MybatisSession.class);
    private SqlSessionFactory sqlSessionFactory;
    private SqlSession sqlSession;

    public MybatisSession(){
        String resource = "mybatis-config.xml";
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            logger.error("The mybatis configuration file doesn't exist.");
        }
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        this.sqlSession = sqlSessionFactory.openSession();
    }

    protected SqlSession getSqlSession() {
        return sqlSession;
    }
}
